<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="编号" prop="ppCode">
        <el-input v-model="queryParams.ppCode" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['abu:polling-project:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['abu:polling-project:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="ppName" />
      <el-table-column label="巡检类型" align="center" prop="ffId" >
       <template slot-scope="scope">
          <div v-if="scope.row.ptSymbol!=null">
            <img   :src="getImage(scope.row.ptSymbol)" style="width: 20px;height: 20px;padding-top: 2px;" />
            <span>{{scope.row.ptName}}</span>
          </div>
         <span v-else>--</span>
       </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="ppCode" />
      <el-table-column label="方法及基准" align="center" prop="ppMethod" />
      <el-table-column label="类型" align="center" prop="ppType" />
      <el-table-column label="上限" align="center" prop="ppUpperLimit" />
      <el-table-column label="下限" align="center" prop="ppLowerLimit" />
      <el-table-column label="可选项" align="center" prop="ppOptions" />
      <el-table-column label="是否正常" align="center" prop="ppNormal" />
      <el-table-column label="是否必须拍照" align="center" prop="ppPicturesState" />
      <el-table-column label="照片" align="center" prop="ppPictures"  >
        <template slot-scope="scope">
           <div v-if="scope.row.ppPictures!=null">
             <img  v-for="(item,index) in scope.row.ppPictures.split(';')" :src="item" style="width: 25px;height: 25px;" />
           </div>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 时间问题  先关闭 删除修改都需要加入判断   -->
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['abu:polling-project:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['abu:polling-project:delete']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="ppName">
              <el-input v-model="form.ppName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="巡检类型" prop="ffId">
              <el-select ref="select" v-model="form.ffId" placeholder="请输入巡检类型" style="width:100%">
                <el-option v-for="item in typesList" :key="item.id" :value="item.id" :label="item.ptName">
                  <img :src="getImage(item.ptSymbol)" style="width: 16px;height: 16px;vertical-align: text-bottom;">
                  {{ item.ptName }}
                </el-option>
              </el-select>


            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="方法及基准" prop="ppMethod">
              <el-input v-model="form.ppMethod" placeholder="请输入方法及基准" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="类型" prop="ppType">
              <el-select v-model="form.ppType" placeholder="请选择类型" style="width: 100%;">
                <el-option label="选项" value="1" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="上限" prop="ppUpperLimit">
              <el-input v-model="form.ppUpperLimit" placeholder="请输入上限" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="下限" prop="ppLowerLimit">
              <el-input v-model="form.ppLowerLimit" placeholder="请输入下限" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="可选项" prop="ppOptions">
              <el-input v-model="form.ppOptions" placeholder="请输入可选项" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否正常" prop="ppNormal">
              <el-radio-group v-model="form.ppNormal">
                <el-radio :label="0">异常</el-radio>
                <el-radio :label="1">正常</el-radio>
              </el-radio-group>

            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否必须拍照" prop="ppPicturesState">
              <el-radio-group v-model="form.ppPicturesState">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="24">
          <el-col :span="24" v-if="form.ppPicturesState == 1">
            <el-form-item label="照片" prop="ppPictures">
              <template v-for="(item,index) in fileList">
                <el-upload :action="uploadImgUrl" list-type="picture-card" name="avatarfile" :show-file-list="false"
                  :headers="headers" style="display: inline-block; vertical-align: top" :before-upload="beforeUpload"
                  :on-change="((val)=>{handleChangeEdit(val, index)})">
                  <img v-if="item" :src="fileList[index]" alt="图片" style="width: 100%" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </template>

              <template v-if="fileList.length<3">
                <el-upload :action="uploadImgUrl" list-type="picture-card" name="avatarFile" :show-file-list="false"
                  :headers="headers" style="display: inline-block; vertical-align: top" :before-upload="beforeUpload"
                  :on-change="handleChange">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    createPollingProject,
    updatePollingProject,
    deletePollingProject,
    getPollingProject,
    getPollingProjectPage,
    exportPollingProjectExcel,getPollingProjectList
  } from "@/api/polling/pollingProject";
  import {
    getPollingTypePage
  } from "@/api/polling/pollingType";

  import {
    getAccessToken
  } from "@/utils/auth";
  export default {
    name: "PollingProject",
    components: {},
    data() {
      return {
        typesList: [],
        uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/abu/polling-project/img", // 上传的图片服务器地址
        headers: {
          Authorization: getAccessToken()
        },
        picUrl: "",
        uploadLoading: false,
        uploadEditLoading: false,
        fileList: [],
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡检项目列表
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          ppCode: null
        },
        // 表单参数
        form: {
          ppPicturesState: 0
        },
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.initData();
      this.getList();
    },
    methods: {
      showImage(){

      },
      getImage(img) {
        return require('@/assets/icons/' + img);
      },
      initData() {
        getPollingTypePage({
          pageNo: 1,
          pageSize: 50
        }).then(res => {
          this.typesList = res.data.list;
        });
      },
      beforeUpload(file) {
        let fileType = file.type;
        if (fileType.indexOf('image') < 0) {
          this.$message.info('请上传图片');
          return false;
        }
        //TODO 验证文件大小
      },
      handleChange(info) {
        if (info.status === 'ready') {
          return
        }
        if (info.status === 'success') {
          let response = info.response;
          if (response.code == 0) {
            this.fileList.push(response.data);
          } else {
            this.$message.info(response.msg);
          }
          this.$forceUpdate();
        }
      },
      handleChangeEdit(info, index) {
        if (info.status === 'ready') {
          return
        }
        this.fileList[index] = "";
        if (info.status === 'success') {
          let response = info.response;
          if (response.code == 0) {
            this.fileList[index] = response.data;
          } else {
            this.$message.info(response.msg);
          }
          this.$forceUpdate();
        }
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        // 执行查询
        getPollingProjectList(this.queryParams).then(response => {
          this.list = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      /** 取消按钮 */
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 表单重置 */
      reset() {
        this.form = {
          ffId: undefined,
          ptId: undefined,
          ppMethod: undefined,
          ppType: undefined,
          ppUpperLimit: undefined,
          ppLowerLimit: undefined,
          ppOptions: undefined,
          ppNormal: undefined,
          ppPicturesState: 0,
          ppPictures: undefined,
          crId: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加巡检项目";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id;
        getPollingProject(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改巡检项目";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.form.ppPictures = this.fileList.join(";");
        this.$refs["form"].validate(valid => {
          if (!valid) {
            return;
          }
          // 修改的提交
          if (this.form.id != null) {
            updatePollingProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            return;
          }
          // 添加的提交
          createPollingProject(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id;
        this.$modal.confirm('是否确认删除巡检项目编号为"' + id + '"的数据项?').then(function() {
          return deletePollingProject(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        // 处理查询参数
        let params = {
          ...this.queryParams
        };
        params.pageNo = undefined;
        params.pageSize = undefined;
        this.$modal.confirm('是否确认导出所有巡检项目数据项?').then(() => {
          this.exportLoading = true;
          return exportPollingProjectExcel(params);
        }).then(response => {
          this.$download.excel(response, '巡检项目.xls');
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
